#include "oled.h"

OLED_Write_Data_t wd;
uint8_t OLED_ADDR = 0;

void OLED_Write_Data(const uint8_t *data, uint16_t len)
{
    wd(OLED_ADDR,0x40,data,len);
}

void OLED_WR_Byte(uint8_t dat, uint8_t cmd)
{
	uint8_t buf = dat;
	if (cmd)
	{   
        wd(OLED_ADDR,0x40,&buf,1);
	}
	else
	{
        wd(OLED_ADDR,0x00,&buf,1);
	}
}

/********************************************
// fill_Picture
********************************************/
void OLED_Fill(uint8_t fill_Data)
{
	uint8_t m;
	uint8_t buf[128];
	memset(buf,fill_Data,128);
	for (m = 0; m < 8; m++)
	{
		OLED_WR_Byte(0xb0 + m, OLED_CMD); //设置页地址
		OLED_WR_Byte(0x00, OLED_CMD);	   //设置列地址低位
		OLED_WR_Byte(0x10, OLED_CMD);	   //设置列地址高位
		
        wd(OLED_ADDR,0x40,buf,128);
	}
}

//坐标设置
void OLED_Set_Pos(uint8_t x, uint8_t y)
{
	OLED_WR_Byte(0xb0 + y, OLED_CMD);
	OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);
	OLED_WR_Byte((x & 0x0f), OLED_CMD);
}

//开启OLED显示
void OLED_Display_On(void)
{
	OLED_WR_Byte(0X8D, OLED_CMD); //SET DCDC命令
	OLED_WR_Byte(0X14, OLED_CMD); //DCDC ON
	OLED_WR_Byte(0XAF, OLED_CMD); //DISPLAY ON
}

//关闭OLED显示
void OLED_Display_Off(void)
{
	OLED_WR_Byte(0X8D, OLED_CMD); //SET DCDC命令
	OLED_WR_Byte(0X10, OLED_CMD); //DCDC OFF
	OLED_WR_Byte(0XAE, OLED_CMD); //DISPLAY OFF
}

//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//size:选择字体 16/12
void OLED_ShowChar(uint8_t x, uint8_t y, char chr, uint8_t Char_Size)
{
	uint8_t c = 0;
	c = chr - ' '; //得到偏移后的值
	if (x > Max_Column - 1)
	{
		x = 0;
		y = y + 2;
	}
	if (Char_Size == 16)
	{
		OLED_Set_Pos(x, y);
		OLED_Write_Data(&F8X16[c * 16],8);
		OLED_Set_Pos(x, y + 1);
		OLED_Write_Data(&F8X16[c * 16+8],8);
	}
	else
	{
		OLED_Set_Pos(x, y);
		OLED_Write_Data(F6x8[c],6);
	}
}

//m^n函数
uint32_t oled_pow(uint8_t m, uint8_t n)
{
	uint32_t result = 1;
	while (n--)
		result *= m;
	return result;
}
//显示2个数字
//x,y :起点坐标
//len :数字的位数
//size:字体大小
//mode:模式	0,填充模式;1,叠加模式
//num:数值(0~4294967295);
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2)
{
	uint8_t t, temp;
	uint8_t enshow = 0;
	for (t = 0; t < len; t++)
	{
		temp = (num / oled_pow(10, len - t - 1)) % 10;
		if (enshow == 0 && t < (len - 1))
		{
			if (temp == 0)
			{
				OLED_ShowChar(x + (size2 / 2) * t, y, ' ', size2);
				continue;
			}
			else
				enshow = 1;
		}
		OLED_ShowChar(x + (size2 / 2) * t, y, temp + '0', size2);
	}
}
//显示一个字符号串
void OLED_ShowString(uint8_t x, uint8_t y, char *chr, uint8_t Char_Size)
{
	uint8_t j = 0;
	while (chr[j] != '\0')
	{
		OLED_ShowChar(x, y, chr[j], Char_Size);
		x += 8;
		if (x > 120)
		{
			x = 0;
			y += 2;
		}
		j++;
	}
}
//显示16X16汉字
void OLED_ShowCHinese(uint8_t x, uint8_t y,const uint8_t *p)
{
	OLED_Set_Pos(x, y);
	OLED_Write_Data(p,16);
	OLED_Set_Pos(x, y + 1);
	OLED_Write_Data(p+16,16);
}

/***********功能描述：显示显示BMP图片128×64起始点坐标(x,y),x的范围0～127，y为页的范围0～7*****************/
void OLED_DrawBMP(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t *p)
{
	unsigned int j = 0;
	uint8_t y;

	for (y = y0; y < y1; y++)
	{
		OLED_Set_Pos(x0, y);

		OLED_Write_Data(&p[j * x1 - x0], x1 - x0);
		j++;
	}
}

void Oled_Init(uint8_t  addr,OLED_Write_Data_t wdcb)
{
    OLED_ADDR = addr;
    wd = wdcb;

	OLED_WR_Byte(0xAE, OLED_CMD); //	AE 关闭显示（睡眠模式）AF正常模式
	OLED_WR_Byte(0x40, OLED_CMD); //	设置显示开始行 (0x00~0x3F)
	OLED_WR_Byte(0x81, OLED_CMD); //	设置对比度命令
	OLED_WR_Byte(0xFF, OLED_CMD); //	对比度的值
	OLED_WR_Byte(0xA1, OLED_CMD); //	0xA0左右反置 0xA1正常
	OLED_WR_Byte(0xC8, OLED_CMD); //	0xc0上下反置 0xc8正常
	OLED_WR_Byte(0xA8, OLED_CMD); //	设置复用率(1 to 64)
	OLED_WR_Byte(0x3F, OLED_CMD); //	--1/64 duty
	OLED_WR_Byte(0xD3, OLED_CMD); //	设置显示偏移 (0x00~0x3F)
	OLED_WR_Byte(0x00, OLED_CMD); //	不偏移
	OLED_WR_Byte(0xD5, OLED_CMD); //	设置显示时钟分频率、振荡器频率
	OLED_WR_Byte(0x80, OLED_CMD); //	时钟分频率 [3:0] 振荡器频率[7:4]
	OLED_WR_Byte(0xD9, OLED_CMD); //	设置重充电周期
	OLED_WR_Byte(0xF1, OLED_CMD); //	第一阶段 [3:0] 第二阶段[7:4]
	OLED_WR_Byte(0xDA, OLED_CMD); //	设置 COM 引脚硬件配置
	OLED_WR_Byte(0x12, OLED_CMD);
	OLED_WR_Byte(0xDB, OLED_CMD); //	设置 Vcomh 取消选择水平
	OLED_WR_Byte(0x40, OLED_CMD); //	调整 Vcomh 调整器输出
	OLED_WR_Byte(0x20, OLED_CMD); //	设置内存地址模式
	OLED_WR_Byte(0x02, OLED_CMD); //	0x00 水平地址模式 0x01 垂直地址模式 0x02 页地址模式
	OLED_WR_Byte(0x8D, OLED_CMD); //--set Charge Pump enable/disable
	OLED_WR_Byte(0x14, OLED_CMD); //--set(0x10) disable
	OLED_WR_Byte(0xA4, OLED_CMD); //	A4 根据RAM内容显示 A5 全部点亮
	OLED_WR_Byte(0xA6, OLED_CMD); //	A6 正常显示 	A7 反相显示
	OLED_WR_Byte(0xAF, OLED_CMD); //	AE 关闭显示（睡眠模式）AF正常模式

	OLED_Fill(0X00);		//清屏
	OLED_Set_Pos(0, 0);
}